#ifndef __VTOR_REG_H__
#define __VTOR_REG_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "vtor_elec_module_config.h"

#ifdef __VTOR_REG__

// 调试时，忽略crc校验
//#define __VtorReg_ConfigIgnoreCrc__

// 定义帧头
#define FRAME_HEADER 				0xAA
#define VtorReg_BaseLen				9

#define VtorReg_RequestRead			0x03
#define VtorReg_ResponseRead		0x04
#define VtorReg_RequestWrite		0x06
#define VtorReg_ResponseWrite		0x07

// frameHead   txId    rxId    cmd  regAddr regNum  regData[regNum]  frameCrc8
//     aa       01 		02 		03	 0000 	0002			         frameCrc8
//     aa       02 		01 		04	 0000 	0002    data[0,1,.....]  frameCrc8

//     aa       01 		02 		06	 0000 	0002    data[0,1,2,...]  frameCrc8
//     aa       02 		01 		07	 0000 	0002			         frameCrc8

typedef struct VtorRegFrameStruct
{
    uint8_t frameHead;
    uint8_t txId;
    uint8_t rxId;
    uint8_t cmd;
    uint8_t regAddr1;
    uint8_t regAddr0;
    uint8_t regNum1;
    uint8_t regNum0;
    uint8_t regBuf[1]; // 指向数据或者crc
}VtorRegFrame;

// 自身地址
extern uint8_t VtorReg_selfId;
extern uint16_t VtorReg_selfRegCnt;
extern uint8_t* VtorReg_selfRegBuf;

// 读写寄存器数据
void VtorReg_WriteSelfReg(uint16_t regAddr, uint16_t regNum, uint8_t* regBuf);
void VtorReg_ReadSelfReg(uint16_t regAddr, uint16_t regNum, uint8_t* regBuf);
void VtorReg_WriteReg(uint8_t rxId, uint16_t regAddr, uint16_t regNum);
void VtorReg_ReadReg(uint8_t rxId, uint16_t regAddr, uint16_t regNum);
// 处理数据回调，做好crc
void VtorReg_Init(uint8_t selfId);
void VtorReg_Transmit(uint8_t* buf, int16_t len);
void VtorReg_Process(const uint8_t* rxFrameBuf, int16_t rxFrameLen);
uint8_t VtorReg_CalcCrc(const uint8_t* dataBuf, int16_t dataLen);

// 测试案例
void VtorReg_WriteRegTest(void);
void VtorReg_ReadRegTest(void);

#endif // __VTOR_REG__

#ifdef __cplusplus
}
#endif

#endif

